package com.challenge.smsweb.admin;

import com.challenge.entity.LoginBean;
import com.challenge.entity.PasswordBean;
import com.challenge.smsbase.Controller.GenericController;
import com.challenge.smsbase.Domain.Result;
import com.challenge.smsbase.util.ResultUtil;
import com.challenge.smsdao.admin.domain.User;
import com.challenge.smsservice.admin.service.UserManager;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * lcc
 */
@RestController
@RequestMapping(value = "user")
public class UserController extends GenericController<User,Long,UserManager> {
    UserManager userManager;

    @Resource
    private Producer producer;

    /**
     * setUserManager
     * @param userManager
     */
    @Autowired
    public void setUserManager(UserManager userManager){
        this.userManager = userManager;
        this.manager = userManager;
    }


    /**
     * 验证码接口
     *
     * @param response
     * @param request
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping(value = "captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到验证码到 session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_DATE, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }
    @PostMapping("login")
    public Result login(@RequestBody LoginBean loginBean, HttpServletRequest request){
        String username = loginBean.getAccount();
        String password = loginBean.getPassword();
        String captcha = loginBean.getCaptcha();
        // 从session中获取之前保存的验证码跟前台传来的验证码进行匹配
        Object kaptcha = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_DATE);
        if (kaptcha == null) {
            return ResultUtil.error("验证码已失效");
        }
        if (!captcha.equals(kaptcha)) {
            return ResultUtil.error("验证码不正确");
        }
        // 用户信息
        User user = userManager.findByName(username);
        // 账号不存在、密码错误
        if (user == null) {
            return ResultUtil.error("用户名或密码错误");
        }
        if (!password.equals(user.getPassword())) {
            return ResultUtil.error("用户名或密码错误");
        }
        return ResultUtil.success("登录成功");

    }
    @PutMapping("updatePassword")
    public Result updatePassword(@RequestBody PasswordBean passwordBean)
    {
        String username = passwordBean.getName();
        String oldPassword = passwordBean.getOldPassword();
        String newPassword = passwordBean.getNewPassword();
        // 用户信息
        User user = userManager.findByName(username);
        if (!user.getPassword().equals(oldPassword)) {
            return ResultUtil.error("密码错误");
        }
        user.setPassword(newPassword);
        userManager.save(user);
        return ResultUtil.success("更新成功");
    }

}